#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QStringList>
#include <QStringListModel>
#include"agreement.h"
#include<QVBoxLayout>
#include<QMap>
#include<QString>
#include<mutex>
#include<QTextEdit>
#include<QThread>

class FileProgress;

namespace Ui {
class mainWindow;
}

class mainWindow : public QWidget
{
    Q_OBJECT

protected:
    void closeEvent(QCloseEvent *event) override; // 重写关闭事件

public:
    explicit mainWindow(QWidget *parent = nullptr);
    ~mainWindow();

    static mainWindow & getInstance();

    void init(std::string);

    //刷新在线好友
    void RefreshFriend(QStringList);
    //文件发送完成
    void startFileUploadFunish(int,std::string);

    void loginRefresh();
    //上传和打包文件
    void uploadFile(std::string,std::string, QString,std::shared_ptr<CloudDrive::UploadControl>,CloudDrive::Packet &,int flag);


private slots:
    void on_refreshFriend_clicked();
    //刷新在线好友
//    void RefreshFriend(QStringList);

    void on_addFriend_clicked();

    //添加好友回复消息框
    void AddFriend(CloudDrive::MessageReturn);
    void FriendApply(std::string);
    void RemoveFriend(CloudDrive::MessageReturn);
    void RemoveFriendREQ(CloudDrive::MessageReturn,std::string);
    void SendMsgShow(std::string,std::string);
    void showFileList(CloudDrive::MesBackFile);
    void creatFileDir(CloudDrive::MessageReturn);
    void deletFile(CloudDrive::MessageReturn);

    //文件
    void startFileWindow(int,std::string,std::shared_ptr<CloudDrive::UploadControl> contral);
    //去除接收到重传的标志
    void uploadAckc(CloudDrive::FileInfo);
    //重传
    void checkAckForTimeouts();
    //dowanloadFile
    void startDownFile(int,std::string,std::string);


    void on_listView_doubleClicked(const QModelIndex &index);

    void on_deletFriend_clicked();

    void on_sendMsg_clicked();

    void on_refreshFile_clicked();

    void on_tableWidget_cellActivated(int row, int column);

    void on_upFIle_clicked();

    void on_newDir_clicked();

    void on_deleteFile_clicked();

    void on_tableWidget_cellClicked(int row, int column);

    void on_tableWidget_itemSelectionChanged();

    void on_loadFile_clicked();

    void on_downFile_clicked();

private:
    Ui::mainWindow *ui;
    std::string userName;

    QStringListModel model;

    QStringList friendList;
    std::mutex friendMutex;
    QMap<QString,QTextEdit*> FriendScreen;

    QVBoxLayout *layout;

    QString currentFriend;

    std::string currentFilePath;
    std::string rootDir;
    std::string curSelectFile;

    //上传和下载的窗口计数
    // 在MainWindow类中
    std::atomic<int> totalSubuploadWindow{0};
    //控制的锁
    QTimer *timer;
    std::map<int,std::shared_ptr<CloudDrive::UploadControl>> windowContral;
    std::mutex windConMutex;

    std::map<std::pair<std::string,uint32_t>,CloudDrive::FileInfoSave> fileData;
    std::mutex fileDataMutex;

    FileProgress *fileDown;

    //下载
    std::atomic<int> totalSubdownWindow{0};

    //分享
    std::atomic<int> totalSubshareWindow{0};


signals:
    void ProgressBar(int,int);
    void startWindow(int,std::string,std::shared_ptr<CloudDrive::UploadControl>);
    void startDownWindow(int,std::string,std::string);

};

#endif // MAINWINDOW_H
